package com.topnav.mc.admin.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.js.lib.exception.CustomException;
import com.topnav.mc.admin.dao.mapper.DictionaryValueMapper;
import com.topnav.mc.admin.entity.DictionaryValue;
import com.topnav.mc.admin.service.IDictionaryValueService;
import com.topnav.mc.base.CommServiceImpl;
@Service
public class DictionaryValueServiceImpl extends CommServiceImpl<DictionaryValueMapper,DictionaryValue> implements IDictionaryValueService{
	@Override
	public boolean beforeSave(DictionaryValue entity){
		if (this.getOne(
				new QueryWrapper<DictionaryValue>()
				.eq("dict_id",entity.getDictId())
				.eq("value1",entity.getValue1()), true) != null){
			throw new CustomException(-1,"字典  %s 已存在",entity.getValue1());
		}
		return true;
	}

	@Override
	public boolean beforeUpdate(DictionaryValue entity){
		QueryWrapper<DictionaryValue> wrapper = new QueryWrapper<DictionaryValue>()
				.eq("dict_id", entity.getDictId())
				.eq("value1",  entity.getValue1())
				.ne("id", entity.getId());
		if (this.getOne(wrapper, true) != null){
			throw new CustomException(-1,"字典类型  %s 已存在",entity.getValue1());
		}
		entity.setUpdateTime(new Date());
		return true;

	}
    /**
     * 查询列表
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
	@Override
	public List<DictionaryValue> list(Wrapper<DictionaryValue> queryWrapper) {
		QueryWrapper<DictionaryValue> wrapper = new QueryWrapper<DictionaryValue>();
		if (queryWrapper.getEntity().getId()!=null){
			wrapper.eq("id", queryWrapper.getEntity().getId());
		}
		if (queryWrapper.getEntity().getDictId()!=null){
			wrapper.eq("dict_id", queryWrapper.getEntity().getDictId());
		}

		if (!StringUtils.isEmpty(queryWrapper.getEntity().getValue1())){
			wrapper.eq("value1", queryWrapper.getEntity().getValue1());
		}

        return getBaseMapper().selectList(wrapper);
    }

    /**
     * 翻页查询
     *
     * @param page         翻页对象
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
	@Override
	public <E extends IPage<DictionaryValue>> E page(E page, Wrapper<DictionaryValue> queryWrapper) {
		QueryWrapper<DictionaryValue> wrapper = new QueryWrapper<DictionaryValue>();

		if (queryWrapper.getEntity().getDictId()!=null){
			wrapper.eq("dict_id", queryWrapper.getEntity().getDictId());
		}
		if (!StringUtils.isEmpty(queryWrapper.getEntity().getValue1())){
			wrapper.like("value1", queryWrapper.getEntity().getValue1());
		}
        return getBaseMapper().selectPage(page, wrapper);
    }
}
